#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include "mylog.h"
#include "getinfo.h"

				

void make_level_to_str(char *str,int level)
{
	switch(level)
	{
		case DEBUG:
			strncpy(str,"DEBUG",strlen("DEBUG")+1);
			break;
		case INFO:
			strncpy(str,"INFO",strlen("INFO")+1);
				break;
		case WARN:
		    strncpy(str,"WARN",strlen("WARN")+1);
				break;
		case ERROR:
		   strncpy(str,"ERROR",strlen("ERROE")+1);
			   break;
		case FATAL:
		  strncpy(str,"FATAL",strlen("FATAL")+1);
			  break;
    }
}
unsigned long get_file_size(char *file_name)
{
	struct stat buf;
	if(stat(file_name,&buf)<0) return 0;
	else return (unsigned long)buf.st_size;
}

int write_log(char *msg,int level)
{
	int fd;

   pthread_mutex_t mutex_conf;
   pthread_mutex_lock(&mutex_conf);
	//获取配置文件信息
   config_info_init();
   init_regist();
   read_config(CONFIG_FILE_PATH);
	    
   config_info* config=get_config();
	pthread_mutex_unlock(&mutex_conf);
   //判断当前级别
	if(level<config->default_level)
	  return LEVEL_LOW;
	
  pthread_mutex_t mutex_outfile;
  pthread_mutex_lock(&mutex_outfile);
  if(strncmp(config->output_file,"STDIN",strlen("STDIN")+1)==0) fd=0;
  else if(strncmp(config->output_file,"STDOUT",strlen("STDOUT")+1)==0) fd=1;
  else if(strncmp(config->output_file,"STDERR",strlen("STDERR")+1)==0) fd=2;
  else
  {
   //判断文件大小

   unsigned long file_size=get_file_size(config->output_file)/1000;
   if(file_size>(unsigned long)config->max_size)

    {
		if(config->save_log)
		{
			//备份名文件，将时间日期作为文件名
			char h_data[LONGSTR]={0};
			time_t timep;
			time(&timep);
			struct tm*p=localtime(&timep);
			sprintf(h_data,"%d%d%d%d%d%d.log",(1900+p->tm_year),(1+p->tm_mon),
						(p->tm_mday),(p->tm_hour),(p->tm_min),(p->tm_sec));
			char new_file_name[LONGSTR]={0};
			strncpy(new_file_name,config->output_file,LONGSTR);
			while(new_file_name[strlen(new_file_name)-1]!='/')
			  new_file_name[strlen(new_file_name)-1]=0;
		    strncat(new_file_name,h_data,strlen(h_data));

	        if(rename(config->output_file,new_file_name)==-1)
                 return SAVE_FILE_ERROR;
		    fd=open(config->output_file,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR);	
		}
		else if(config->save_log==0)
		  fd=open(config->output_file,O_TRUNC|O_WRONLY);
		
	}
   else fd=open(config->output_file,O_APPEND|O_WRONLY);
  }
   if (fd==-1) return OPEN_FILE_ERROR;
	//根据configinfo中的format值获取其它信息
  message_info msginfo;
  get_info(&msginfo,config->format);
  
  int p=0;int sizet;
  while(config->format[p]!=-1)
  {
	  if(config->format[p]==0)
	  {
		  sizet=write(fd,msginfo.data,strlen(msginfo.data));
		  sizet=write(fd,"   ",3);
	  }
	  else if(config->format[p]==1)
	  {
		  sizet=write(fd,msginfo.time,strlen(msginfo.time));
		  if(sizet==-1) perror("error"); 
		  sizet=write(fd,"   ",3);
	  }
	  else if(config->format[p]==2)
	  {
		  sizet=write(fd,msginfo.file_name,strlen(msginfo.file_name));
		  sizet=write(fd,"   ",3);
	  }
	  else if(config->format[p]==3)
	  {
		//  sizet=write(fd,MakeLevelToStr(level),strlen(MakeLevelToStr(level)));
     char levstr[10]={0};;
	 make_level_to_str(levstr,level);
	 sizet=write(fd,levstr,strlen(levstr));
	  
	      sizet=write(fd,"    ",3);
	  }
	  else if(config->format[p]==4)
	  {
		  sizet=write(fd,msg,strlen(msg));
		 sizet= write(fd,"    ",3);
	  }
	  p++;
  }
  char c='\n';
  sizet=write(fd,&c,1);

  if(fd>3) close(fd);
  pthread_mutex_unlock(&mutex_outfile);
  return 0;
}

